<?php
  class cacher{
    private static $_cache_dir = '/cache/';
    private $_cache_age = '60';

	function __construct(){
		echo getcwd() . self::$_cache_dir;
		if( !is_dir(getcwd() . self::$_cache_dir) )
			mkdir(getcwd() . self::$_cache_dir);
	}

    function save($data, $uid=null){
	   $write_cache = true;
	   if( empty($uid) )
			$uid = $_SERVER[SCRIPT_NAME];
			
	   $uid = md5($uid);
	   $path = getcwd() . self::$_cache_dir . $uid;
	   $cache_age;
	   $file_mod;
      
      	if( file_exists($path) ){
			$file_mod = filemtime($path);
			$cache_age = time() - $file_mod;
			if( $cache_age > $this->_cache_age )
				$write_cache = true;
			else
 				$write_cache = false;
      	}
      
      
     	if( !empty($data) && $write_cache){
			file_put_contents(getcwd() . self::$_cache_dir . $uid, $data, LOCK_EX);
			echo "<br>Cache Written";
    	}
    }

    function get($uid=null){
      if( empty($uid) )
$uid = $_SERVER[SCRIPT_NAME];
      $uid = md5($uid);
      return file_get_contents(getcwd() . self::$_cache_dir . $uid);
    }
    function printInfo(){
      return "<pre>" . print_r($_SERVER, true) . "</pre>";
    }
  }
?> 